# Load Data
setwd("/users/josephphillips/Dropbox/COVID-19/Data/FC Data Repository")
df <- read.csv("UK.csv",header=T,sep=",")

# Load GLMNet Function
run_LASSO <- function(data, yvar, pre){
  
  set.seed(2334)
  
  var_quo <- enquo(yvar)
  var_quo1 <- enquo(pre)
  
  ## select x variables 
  dat <- data %>%
    dplyr::select(university, agegroup, male, married, frequentchurch, profile_GOR, left, right, ideology1, knowledge, nonwhite, polinterest, trust_healthgov, total_media_trust, lives_highincidence)
  
  pre <- data %>%
    dplyr::select(!!var_quo1) %>% pull() # Save as a vector instead of a data frame ot use model matrix
  
  ## select y-variable
  out1 <- data %>% 
    dplyr::select(!!var_quo) %>% pull() # save as a vector instead of a data-frame to use model.matrix()
  
  merged1 <- cbind(out1, dat, pre) %>% na.omit() ## merge and delete NA
  x1 <- model.matrix(out1 ~ ., data = merged1) ## make model matrix
  y1 <- merged1$out1 ## select y-variable from NA-deleted dataset
  mod1 <- cv.glmnet(x1, y1, alpha = 1, family = "gaussian") ## run lasso model
  coef <- coef(mod1) ## get coef  
  
  list(merged1, x1, y1, mod1, coef)
  
}

run_LASSO_nopre <- function(data, yvar){
  
  set.seed(2334)
  
  var_quo <- enquo(yvar)
  
  ## select x variables 
  dat <- data %>%
    dplyr::select(university, agegroup, male, married, frequentchurch, profile_GOR, left, right, ideology1, knowledge, nonwhite, polinterest, trust_healthgov, total_media_trust, lives_highincidence)
  
  ## select y-variable
  out1 <- data %>% 
    dplyr::select(!!var_quo) %>% pull() # save as a vector instead of a data-frame to use model.matrix()
  
  merged1 <- cbind(out1, dat) %>% na.omit() ## merge and delete NA
  x1 <- model.matrix(out1 ~ ., data = merged1) ## make model matrix
  y1 <- merged1$out1 ## select y-variable from NA-deleted dataset
  mod1 <- cv.glmnet(x1, y1, alpha = 1, family = "gaussian") ## run lasso model
  coef <- coef(mod1) ## get coef  
  
  list(merged1, x1, y1, mod1, coef)
  
}

# Lasso Regressions
## W1
W1_tfc_uk <- run_LASSO_nopre(df,targeted_false) # university, age 35, left, ideology1, knowledge, nonwhite, polinterest, trust_healthgov, total_media_trust
W1_nfc_uk <- run_LASSO_nopre(df,nontargeted_false) # university, left, ideology1, knowledge, nonwhite, polinterest, trust_healthgov, total_media_trust
W1_nfc_uk_noleak <- run_LASSO_nopre(df,nontargeted_false_noleak) # knowledge, nonwhite, health trust
W1_ntc_uk <- run_LASSO_nopre(df,nontargeted_true) # male, knowledge, polinterest, trust_healthgov, total_media_trust
## Targeted False
targeted_false.w2 <- run_LASSO(df,W2_targeted_false,pre=targeted_false) ## ideology1, knowledge, trust_healthgov, pre
targeted_false.w3 <- run_LASSO(df,W3_targeted_false,pre=targeted_false) ## ideology1, knowledge, trust_healthgov, pre
con_theory1.w2 <- run_LASSO(df,W2_accuracy_con_theory1,pre=accuracy_con_theory1) ## trust_healthgov, pre
con_theory5.w2 <- run_LASSO(df,W2_accuracy_con_theory5,pre=accuracy_con_theory5) ## ideology1, knowledge, trust_healthgov, pre
False1.w2 <- run_LASSO(df,W2_accuracy_False1,pre=accuracy_False1) ## pre
False4.w2 <- run_LASSO(df,W2_accuracy_False4,pre=accuracy_False4) ## ideology1, knowledge, pre
con_theory1.w3 <- run_LASSO(df,W3_accuracy_con_theory1,pre=accuracy_con_theory1) ## ideology1, trust_healthgov, pre
con_theory5.w3 <- run_LASSO(df,W3_accuracy_con_theory5,pre=accuracy_con_theory5) ## knowledge, trust_healthgov, pre
False1.w3 <- run_LASSO(df,W3_accuracy_False1,pre=accuracy_False1) ## pre
False4.w3 <- run_LASSO(df,W3_accuracy_False4,pre=accuracy_False4) ## university, ideology1, knowledge, trust_healthgov, pre
## Non-Targeted False
nontargeted_false.w2 <- run_LASSO(df,W2_nontargeted_false,pre=nontargeted_false) ## pre
nontargeted_false.w3 <- run_LASSO(df,W3_nontargeted_false,pre=nontargeted_false) ## Ideo1, Knowledge, Health Trust, pre
nontargeted_false.w2.noleak <- run_LASSO(df,W2_nontargeted_false_noleak,pre=nontargeted_false_noleak) ## Pre
nontargeted_false.w3.noleak <- run_LASSO(df,W3_nontargeted_false_noleak,pre=nontargeted_false_noleak) ## Knowledge, Health Trust, pre
False2.w2 <- run_LASSO(df,W2_accuracy_False2,pre=accuracy_False2) ## pre
edge1.w2 <- run_LASSO(df,W2_accuracy_edge1,pre=accuracy_edge1) ## pre
con_theory2.w2 <- run_LASSO(df,W2_accuracy_con_theory2,pre=accuracy_con_theory2) ## left, ideology1, trust_healthgov, pre
f_cure1.w2 <- run_LASSO(df,W2_accuracy_f_cure1,pre=accuracy_f_cure1) ## pre
f_cure2.w2 <- run_LASSO(df,W2_accuracy_f_cure2,pre=accuracy_f_cure2) ## pre
False2.w3 <- run_LASSO(df,W3_accuracy_False2,pre=accuracy_False2) ## pre
edge1.w3 <- run_LASSO(df,W3_accuracy_edge1,pre=accuracy_edge1) ## pre
con_theory2.w3 <- run_LASSO(df,W3_accuracy_con_theory2,pre=accuracy_con_theory2) ## ideology1, trust_healthgov, pre
f_cure1.w3 <- run_LASSO(df,W3_accuracy_f_cure1,pre=accuracy_f_cure1) ## knowledge, trust_healthgov, pre
f_cure2.w3 <- run_LASSO(df,W3_accuracy_f_cure2,pre=accuracy_f_cure2) ## knowldge, trust_healthgov, pre
## Non-Targeted True
nontargeted_true.w2 <- run_LASSO(df,W2_nontargeted_true,pre=nontargeted_true) ## Health Trust, pre
nontargeted_true.w3 <- run_LASSO(df,W3_nontargeted_true,pre=nontargeted_true) ## Health Trust, Pre
True1.w2 <- run_LASSO(df,W2_accuracy_True1,pre=accuracy_True1) ## trust_healthgov, pre
True2.w2 <- run_LASSO(df,W2_accuracy_True2,pre=accuracy_True2) ## trust_healthgov, pre
True3.w2 <- run_LASSO(df,W2_accuracy_True3,pre=accuracy_True3) ## trust_healthgov, pre
True4.w2 <- run_LASSO(df,W2_accuracy_True4,pre=accuracy_True4) ## trust_healthgov, pre
True5.w2 <- run_LASSO(df,W2_accuracy_True5,pre=accuracy_True5) ## pre
True6.w2 <- run_LASSO(df,W2_accuracy_True6,pre=accuracy_True6) ## trust_healthgov, pre
change3.w2 <- run_LASSO(df,W2_accuracy_change3,pre=accuracy_change3) ## ideology1, trust_healthgov, pre
change1.w2 <- run_LASSO(df,W2_accuracy_change1,pre=accuracy_change1) ## male, ideology1, knowledge, polinterest, pre
change4.w2 <- run_LASSO(df,W2_accuracy_change4,pre=accuracy_change4) ## age 65, knowledge, nonwhite, polinterest, trust_healthgov, pre
True1.w3 <- run_LASSO(df,W3_accuracy_True1,pre=accuracy_True1) ## trust_healthgov, pre
True2.w3 <- run_LASSO(df,W3_accuracy_True2,pre=accuracy_True2) ## trust_healthgov, pre
True3.w3 <- run_LASSO(df,W3_accuracy_True3,pre=accuracy_True3) ## trust_healthgov, pre
True4.w3 <- run_LASSO(df,W3_accuracy_True4,pre=accuracy_True4) ## trust_healthgov, pre
True5.w3 <- run_LASSO(df,W3_accuracy_True5,pre=accuracy_True5) ## Male, trust_healthgov, pre
True6.w3 <- run_LASSO(df,W3_accuracy_True6,pre=accuracy_True6) ## trust_healthgov, pre
change3.w3 <- run_LASSO(df,W3_accuracy_change3,pre=accuracy_change3) ## trust_healthgov, pre
change1.w3 <- run_LASSO(df,W3_accuracy_change1,pre=accuracy_change1) ## male, pre
change4.w3 <- run_LASSO(df,W3_accuracy_change4,pre=accuracy_change4) # knowledge, polinterest, trust_healthgov, total_media_trust, pre